<?php

//------------------------
// 物流公司控制器
//-------------------------
namespace app\admin\controller;

use app\common\controller\AdminBase;
use app\common\model\Shipping as ShippingModel;


class Shipping extends AdminBase
{
    protected $ShippingModel;

    protected function _initialize()
    {
        parent::_initialize();
        $this->ShippingModel       = new ShippingModel();
    }


    // 列表
    public function index(){
        $shipping_name    = $this->request->param('shipping_name','','trim');

        $where = array();

        if ($shipping_name) {
             $where['shipping_name'] = array('like','%'.$shipping_name.'%');
        }

        $info = $this->ShippingModel->where($where)->
          paginate(10,false,['query' => [
            'shipping_name'  =>$shipping_name
            ] ]);

        $this->assign('shipping_name',$shipping_name);
        $this->assign('info',$info);

        return $this->fetch('index');
    }


    // 添加页面
    function add(){
        return $this->fetch('add');
    }

    /**
     * @desc 添加操作
     */
    public function addProc() {
        $data            = $this->request->param();
        //使用AdminUser验证器的add场景
        $validate_result = $this->validate($data, 'Shipping');

        if ($validate_result !== true) {
                $this->error($validate_result);
        }

        $res = $this->ShippingModel->save($data);

        if ($res === false) {
            $this->error($this->ShippingModel->getError());
        } else {
            $this->success('添加成功');
        }
    }

    // 编辑页面
    function edit(){
        $id = $this->request->param('id',0,'intval');

        $data = $this->ShippingModel->get($id);

        $this->assign('data',$data);
        return $this->fetch('edit');
    }

    // 编辑操作
    function editProc(){
        $data  = $this->request->param('');

        $validate_result = $this->validate($data, 'Shipping');

        if ($validate_result !== true) {
                $this->error($validate_result);
        }

        // allowField表示过滤非数据表中字段的数据
        $res = $this->ShippingModel->allowField(true)->save($data,$data['id']);

        if ($res === false) {
            $this->error($this->ShippingModle->getError());
        } else {
            $this->success('保存成功');
        }
    }



    // 删除操作
    public function delete(){
        $id = $this->request->param('id',0,'intval');

        $where['id'] = array('eq',$id);

        $res = $this->ShippingModel->where($where)->delete();

        if ($res === false) {
            $this->error($this->ShippingModel->getError());
        } else{
            $this->success('删除成功');
        }
    }








}